package com.tarzan.system.controller;


import com.tarzan.api.model.request.menu.InsertMentReq;
import com.tarzan.api.model.request.menu.SysMenuReq;
import com.tarzan.api.model.request.menu.UpdateMentReq;
import com.tarzan.api.model.response.ment.SysMenuResp;
import com.tarzan.common.core.base.BaseResponse;
import com.tarzan.common.core.exception.BusinessException;
import com.tarzan.system.service.ISysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author liujinshan
 * @since 2020-07-09
 */
@RestController
@RequestMapping("menu")
@Slf4j
public class SysMenuController {


    @Autowired
    private ISysMenuService iSysMenuService;


    /**
     * 根据用户查询用户对应的菜单
     * @param menu
     * @return
     */
    @GetMapping("list")
    public BaseResponse<List<SysMenuResp>> list(SysMenuReq menu)
    {
        BaseResponse<List<SysMenuResp>> response = new BaseResponse<>();
        try{
            Long userId = 1L;
            response = BaseResponse.ok(iSysMenuService.selectMenuList(menu, userId));
        } catch (BusinessException e) {
            response.setErrCode(e.getCode());
            response.setErrMsg(e.getMsg());
            log.info("SysMenuController -> list() BusinessException ex="+e);
        } catch (Exception e) {
            response.setErrCode("99999");
            response.setErrMsg(e.getMessage());
            log.error("SysMenuController -> list() Exception e="+e);
        }
        return response;
    }


    /**
     * 更具id查询菜单详情
     * @param menuId
     * @return com.jinshan.happy.base.BaseResponse<com.jinshan.happy.vo.response.SysMenuResp>
     * @Date 2020/9/30 14:43
     * @Author liujinshan
     **/
    @GetMapping("getMenuDetail/{menuId}")
    public BaseResponse<SysMenuResp> getMenuDetail(@PathVariable Long menuId){
        BaseResponse<SysMenuResp> response = new BaseResponse<>();
        try{
            response = BaseResponse.ok(iSysMenuService.findMenuDetailById(menuId));
        } catch (BusinessException e) {
            response.setErrCode(e.getCode());
            response.setErrMsg(e.getMsg());
            log.info("SysMenuController -> getMenuDetail() BusinessException ex="+e);
        } catch (Exception e) {
            response.setErrCode("99999");
            response.setErrMsg(e.getMessage());
            log.error("SysMenuController -> getMenuDetail() Exception e="+e);
        }
        return response;
    }


    /**
     * 新增菜单
     * @param insertMenuReq
     * @return com.jinshan.happy.base.BaseResponse
     * @Date 2020/9/30 15:12
     * @Author liujinshan
     **/
    @PostMapping(value = "insert" ,produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse add(@RequestBody InsertMentReq insertMenuReq){
        try{
            iSysMenuService.insertMenu(insertMenuReq);
        } catch (BusinessException e) {
            log.info("SysMenuController -> add() BusinessException ex="+e);
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            log.error("SysMenuController -> add() Exception ex="+e);
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }

    /**
     * 更新菜单
     * @param updateMentReq
     * @return com.jinshan.happy.base.BaseResponse
     * @Date 2020/9/30 15:51
     * @Author liujinshan
     **/
    @PutMapping(value = "update" ,produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse update(@RequestBody UpdateMentReq updateMentReq){
        try{
            iSysMenuService.update(updateMentReq);
        } catch (BusinessException e) {
            log.info("SysMenuController -> update() BusinessException ex="+e);
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            log.error("SysMenuController -> update() Exception ex="+e);
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }


    /**
     * 删除菜单
     * @param menuId
     * @return com.jinshan.happy.base.BaseResponse
     * @Date 2020/10/9 8:39
     * @Author liujinshan
     **/
    @DeleteMapping(value = "delete/{menuId}")
    public BaseResponse delete(@PathVariable Long menuId){
        try{
            iSysMenuService.delete(menuId);
        } catch (BusinessException e) {
            log.info("SysMenuController -> update() BusinessException ex="+e);
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            log.error("SysMenuController -> update() Exception ex="+e);
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }

    /**
     * @title selectMenuPermsByUserId
     * @description TODO
     * @param userId
     * @return com.tarzan.common.core.base.BasePaginationResponse 
     * @author liujinshan
     * @data 2021/7/2 10:30
     */
    @GetMapping(value = "selectMenuPermsByUserId/{userId}")
    public BaseResponse<List<String>> selectMenuPermsByUserId(@PathVariable Long userId) {
        BaseResponse<List<String>> response = new BaseResponse<>();
        try{
            response = BaseResponse.ok(iSysMenuService.selectMenuPermsByUserId(userId));
        } catch (BusinessException e) {
            log.info("SysMenuController -> selectMenuPermsByUserId() BusinessException ex="+e);
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            log.error("SysMenuController -> selectMenuPermsByUserId() Exception ex="+e);
            return BaseResponse.error("500",e.getMessage());
        }
        return response;
    }

}
